package attack.yefi;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

import framework.AnimationManager;
import framework.BasicFighter;
import framework.Util;
import framework.attack.AnimationAttack;
import framework.movements.LinearMovement;

public class Hook extends AnimationAttack {

	private Image hook;

	private final static int MAX_WIDTH = 600;
	
	private final static int WIND_UP = 20;
	
	private int lastHit = 0;
	
	private int hookWidth;
	
	boolean forward;

	private int hitTime;
	
	private BasicFighter target;

	public Hook(BasicFighter attacker) {
		super(attacker);
		AnimationManager n = attacker.getAnimations();
		this.spriteSheet = n.getSpriteSheet();
		Image[] frames = new Image[3];
		frames[0] = this.spriteSheet.getSprite(0, 2);
		frames[1] = this.spriteSheet.getSprite(1, 2);
		frames[2] = this.spriteSheet.getSprite(1, 2);

		hook = this.spriteSheet.getSprite(1, 5);

		this.attackAnimation = new Animation(frames, 150);
		this.attackAnimation.setDuration(2, 300);
		this.attackAnimation.setLooping(false);
		this.attackAnimationBackward = Util.getFlippedAnimation(this.attackAnimation, true, false);
		this.attackAnimationBackward.setLooping(false);
		this.attacker = attacker;
		this.isJumpingAuthorized = true;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
	}

	@Override
	public void draw(Graphics g) {
		super.draw(g);
		int width = (int) getAttacker().getHitBox().getWidth();
		if (!hasHit) {
			this.hookWidth = (int) (MAX_WIDTH * getScale() * currentTime / getDuration());
		} else {
			this.hookWidth = (int) (Math.abs(target.getHitBox().getCenterX() - getAttacker().getHitBox().getCenterX()));
		}
		
		int x = (int) (forward ? getX() + getAttacker().getWidth() - width * 2.5f :
			                                getX() + width * 2.5f - this.hookWidth);
		
		if (attackAnimationUsed.getFrame() == 2) {
			hook.draw(x, getAttacker().getY(), hookWidth, getAttacker().getHeight());
		}	
	}

	@Override
	public void trigger() {
		super.trigger();
		setX(getAttacker().getX());
		getAttacker().interruptJump();
		lastHit = -WIND_UP;
	    forward = getAttacker().isForward();
	    hookWidth = 0;
	}

	@Override
	public Rectangle getHitBox() {
		int height = (int) (getAttacker().getHitBox().getWidth() / 2);
		int y = (int) (getAttacker().getHitBox().getCenterY() - width / 2);
		int width = (int) (MAX_WIDTH * currentTime * getScale() / getDuration());
		int x = forward ? getX() + getAttacker().getWidth() - height * 6 :
			                                getX() + height * 6 - width;
		Rectangle r = null;
		if (attackAnimationUsed.getFrame() == 2) {
			r = new Rectangle(x, y, width, height);
		}
		return r;
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		int forward = f.isForward() ? 1 : -1;
		if (f.isAttacking() && f.getCurrentAttack().isInterruptable()) {
			f.getCurrentAttack().interrupt();
		}
		if (!hasHit) {
			hitTime = currentTime;
			target = f;
			LinearMovement m = new LinearMovement(f, new Point(getAttacker().getX() - (getAttacker().getHeight() / 2 * forward), f.getY()));
			m.setDuration(150);
			f.move(m);
			
			m = new LinearMovement(getAttacker(), new Point(getAttacker().getX(), getAttacker().getY()));
			m.setDuration(150);
			getAttacker().move(m);
			f.hit(getPower(), 150);
			lastHit = currentTime;
		}
		hasHit = true;
	}

	@Override
	public int getPower() {
		return 5;
	}
	
	@Override
	public int getDuration() {
		return !hasHit() ? 600 : hitTime + 150;
	}
}
